<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
    	$username = cookie('username');
    	$name = cookie('name');
    	$usermodel = D('user');
    	if ($username == '' || $name =='') {
    		$this->redirect('Home/index/login');
    		return false;
    	} 
    	$data = $usermodel->where("username='$username'")->find();
    	$truename = md5($data['username'].$data['password']);
    	if ($truename == $name) {
    		$member = D('member');
    		$num = $member->count();//会员总人数
            $this->assign('num',$num);
    		$start =strtotime(date('Y-m-d 00:00:00'));//今天时间戳
    		$end = strtotime(date('Y-m-d 23:59:59')); //今天时间戳   		
    		$map['pubtime'] = array('between' , array($start,$end));
    		$todaynum = $member->where($map)->count();//今日新增会员
            $todaynum = $todaynum == '' ? 0 : $todaynum;
            $this->assign('todaynum',$todaynum);
    		$pay = D('pay');
    		$todaymoney = $pay->where($map)->sum('money');//今日消费金额
            $todaymoney = $todaymoney == '' ? 0 : $todaymoney;
            $this->assign('todaymoney',$todaymoney);
            $zstart = strtotime(date('Y-m-d 00:00:00').'-1 day'); //昨天时间戳        
    		$zend = strtotime(date('Y-m-d 23:59:59').'-1 day');//昨天时间戳
            $map['pubtime'] = array('between' , array($zstart,$zend));
            $zmoney = $pay->where($map)->sum('money');//昨天消费金额
            $zmoney = $zmoney == '' ? 0 : $zmoney;
            $this->assign('zmoney',$zmoney);
            $map['pubtime'] = array('between' , array($start,$end));
            $map['name'] = array('eq','现金用户');
            $todayxjmoney = $pay->where($map)->sum('money');//今日收入现金金额
            $todayxjmoney = $todayxjmoney == '' ? 0 : $todayxjmoney;
            $this->assign('todayxjmoney',$todayxjmoney);
            $map['pubtime'] = array('between' , array($zstart,$zend));
            $zxjmoney = $pay->where($map)->sum('money');//昨日收入现金金额
            $zxjmoney = $zxjmoney == '' ? 0 : $zxjmoney;
            $this->assign('zxjmoney',$zxjmoney);
            $map = array();
            $map['pubtime'] = array('between' , array($start,$end));
            $cdk = D('cdk');
            $todaycz = $cdk->where($map)->sum('money');//今日充值金额
            $todaycz = $todaycz == '' ? 0 : $todaycz;
            $this->assign('todaycz',$todaycz);
            $map['pubtime'] = array('between' , array($zstart,$zend));
            $zcz = $cdk->where($map)->sum('money');//昨日充值金额
            $zcz = $zcz == '' ? 0 : $zcz;
            $this->assign('zcz',$zcz);
    		$this->display();
    	} else {
    		$this->redirect('Home/index/login');
    		return false;
    	}
        
    }    
 /**
  * 新增会员
  * @return [type] [description]
  */
    public function addmember() {
    	if (!IS_POST) {
    		$this->display();
    	} else {
    		if (I('post.pay_password') == C('PAY_PASSWORD')){
    			$member = D('member');
	    		$_POST['pubtime'] = time();
	    		if ($member->create()) {
	    			$result = $member->add();
	    			if ($result) {
	    				$this->success('会员添加成功');
	    			} else {
	    				$this->error('会员添加失败');
	    			}
	    		}
    		} else {
    			$this->error('密码错误，无法添加');
    		}
    		

    	}
    }
/**
 * 会员信息查看
 * @return [type] [description]
 */
	public function listmember() {
		$member = D('member');
        $count  = $member->count();// 查询满足要求的总记录数
        $Page   = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show   = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $data = $member->order('pubtime')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('page',$show);// 赋值分页输出
		$this->assign('data' , $data);
		$this->display();
	}
 /**
  * 会员修改
  * @return [type] [description]
  */
 	public function editmember($mid) {
 		$member = D('member');
 		if (!IS_POST) {
 			$data = $member->find($mid);
 			$this->assign('data' , $data);
    		$this->display();
    	} else { 
    		if ($member->where("mid='$mid'")->save($_POST)) {    	
    			$this->success('会员修改成功');    				
    		} else {
    			$this->error('会员修改失败');
    		}
    	}
 	}
 /**
  * 会员删除
  * @return [type] [description]
  */
 	public function delmember($mid) {
 		$member = D('member');
 		if ($member->delete($mid)) {
 			$this->redirect('Home/index/listmember');
 		} 
 	}
 /**
  * 储值卡充值
  * @return [type] [description]
  */
 	public function chongzhi() {
 		if (!IS_POST) {
 			$this->display();
 		} else {
 			if (I('post.pay_password') == C('PAY_PASSWORD')){
 			$member = D('member');
 			$phone = I('post.phone');
 			$data = $member->where("phone='$phone'")->find();
 			$money = I('post.money');
 			$data['money'] = $money + $data['money'];
 			if ($member->where("mid='$data[mid]'")->save($data)) {
 				$cdk = D('cdk');
 				$arr['phone'] = $phone;
 				$arr['name'] = I('post.name');
 				$arr['money'] = $money;
 				$arr['pubtime'] = time();
 				$cdk->add($arr);
 				$this->success('充值成功！');
 			} else {
 				$this->error('充值失败！');
 			}} else {
 				$this->error('密码错误，无法添加');
 			}

 		}
 	}
 /**
  * ajaxYz
  * @return [type] [description]
  */
 	public function ajaxYz(){
 		$phone = $_POST['phone'];
 		$member = D('member');
 		$data = $member->where("phone='$phone'")->find();
 		echo $data['name'];
 	}
  /**
  * ajaxYz
  * @return [type] [description]
  */
 	public function ajaxPay(){
 		$phone = $_POST['phone'];
 		$member = D('member');
 		$data = $member->where("phone='$phone'")->find();
 		echo $data['name'].'-'.$data['discount'].'-'.$data['money'];
 	}
 /**
  * ajaxShop
  * @return [type] [description]
  */
 	public function ajaxShop(){
 		$sid = $_POST['sid'];
 		$shop = D('shop');
 		$data = $shop->where("sid='$sid'")->find();
 		echo $data['price'];
 		// $member = D('member');
 		// $data = $member->where("phone='$phone'")->find();
 		//echo $data['name'].'-'.$data['discount'].'-'.$data['money'];
 	}

 /**
  * 快捷支付
  * @return [type] [description]
  */
 	public function pay() {
 		$shop = D('shop');
 		if (!IS_POST) {
 			$data = $shop->select();
 			$this->assign('data' , $data);
 			$this->display();
 		} else {
 			$member = D('member');
 			$xjmoney = I('post.xjmoney'); 			
 			//判断是否是现金支付
 			if ($xjmoney == '') {
 				//说明他是用储值卡支付
 				$phone = I('post.phone');
 				$data = $member->where("phone='$phone'")->find();
 				$money = $data['money'] - I('post.price') * I('post.num')*$data['discount']/100;
 				if ($money < 0) {
 					$this->error('账户余额过低，请及时充值再进行购买！');
 					return false;
 				} else {
 					$data['money'] = $money;
 					if($member->where("phone='$phone'")->save($data)) {
 						$pay = D('pay');
 						$arr['name'] = I('post.name');
 						$arr['shopname'] = I('post.level');
 						$arr['money'] = I('post.price') * I('post.num')*$data['discount']/100;
 						$arr['pubtime'] = time();
 						$pay->add($arr);
 						$this->success('购买成功！请及时配货');
 					} else {
 						$this->error('购买失败！请查看原因');
 					}
 				}
 			} else {
 				//说明他是现金支付
 				$money = I('post.xjmoney') - I('post.price') * I('post.num');
 				if ($money < 0) {
 					$this->error('现金不够啊，请及时充值再进行购买！');
 					return false;
 				} else { 					
					$pay = D('pay');
					$arr['name'] = '现金用户';
					$arr['shopname'] = I('post.level');
					$arr['money'] = I('post.price') * I('post.num');
					$arr['pubtime'] = time();
					$pay->add($arr);
					$this->success('购买成功！请及时配货'); 					
 				}
 			}
 		}
 	}
    public function login(){
    	if (!IS_POST) {
    		$this->display();
    	} else {
        print_r($_POST);
    		$username = I('post.username');
    		$password = I('post.password');
    		$user = D('user');
    		$data = $user->where("username = '$username' and password = '$password'")->find();
        dump($data);
    		if ($data == '') {
    			$this->error('登陆错误，请检查用户名或者密码。');
    		} else {
    			cookie('name' , md5($username.$password));
    			cookie('username' , $username);
    			$this->redirect('Home/index/index');
    		}
    	}
    	
    }
    public function logout(){
    	cookie('name',null);
    	cookie('username',null);
    	$this->redirect('Home/index/index');
    }

}